//
//  CYTopViewController.m
//  CYTabBarViewController
//
//  Created by lc4y on 2017/12/4.
//  Copyright © 2017年 weiwang. All rights reserved.
//

#import "CYTopViewController.h"
#import "CYTopTitleCollectionViewCell.h"

@interface CYTopViewController ()

@property (nonatomic,strong) UIView *lineView;

@end

@implementation CYTopViewController {
    BOOL _didSelected;
    CYTopTitleCollectionViewCell *_preTopTitleCollectionViewCell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [_cy_collectionView registerNib:[UINib nibWithNibName:@"CYTopTitleCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CYCollectionCell];
    _cy_collectionView.backgroundColor = [UIColor clearColor];
    
    [self configSubViews];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    _preTopTitleCollectionViewCell.titleButton.selected = NO;
    _preTopTitleCollectionViewCell = (CYTopTitleCollectionViewCell *)[_cy_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_cy_currentIndex inSection:0]];
    _preTopTitleCollectionViewCell.titleButton.selected = YES;
}

- (void)configSubViews {
    [self.view addSubview:[CYTitleView sharedTitleView]];
    [[CYTitleView sharedTitleView] addSubview:_cy_collectionView];
    self.lineView.backgroundColor = [CYTitleView sharedTitleView].cy_lineColor;
    [_cy_collectionView addSubview:self.lineView];
    [self cy_configLayoutSubViews];
}

- (void)setContentArray:(NSArray *)contentArray {
    [super setContentArray:contentArray];
    
    UIViewController *viewController = [_cy_dataArray firstObject];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(2);
        make.top.mas_equalTo(48);
        make.width.mas_equalTo([self stringWidth:viewController.cy_tabBarItem.cy_titleN]);
    }];
}

- (void)cy_configLayoutSubViews {
    [super cy_configLayoutSubViews];
    [[CYTitleView sharedTitleView] mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, _itemSpace, 0, _itemSpace);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return _itemSpace;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return _itemSpace;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIViewController *veiwController = _cy_dataArray[indexPath.row];
    CGFloat width = [self stringWidth:veiwController.cy_tabBarItem.cy_titleN];
    if(veiwController.cy_tabBarItem.cy_normalImageN){
        UIImage *image = [UIImage imageNamed:veiwController.cy_tabBarItem.cy_normalImageN];
        width += image.size.width + 5;
    }
    return CGSizeMake(width, 40);
}

- (void)cy_refreshCollectionCell:(UICollectionViewCell *)collectionCell indexPath:(NSIndexPath *)indexPath {
    CYTopTitleCollectionViewCell *topTitleCell = (CYTopTitleCollectionViewCell *)collectionCell;
    [topTitleCell refreshCollectionCell:_cy_dataArray[indexPath.row]];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.cy_currentIndex = indexPath.item;
    _didSelected = YES;
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(topViewController)]) {
        [self.delegate topTitleViewMenuClick:indexPath.item];
    }
}

- (void)setCy_currentIndex:(NSInteger)cy_currentIndex {
    if( cy_currentIndex >= _cy_dataArray.count ) {
        return;
    }
    
    _cy_currentIndex = cy_currentIndex;
    NSInteger width = _itemSpace;
    NSInteger lineWidth = 0;
    for (NSInteger idx = 0; idx < cy_currentIndex; idx++) {
        UIViewController *viewController = _cy_dataArray[idx];
        width = width + [self stringWidth:viewController.cy_tabBarItem.cy_titleN] + _itemSpace;
        
        if( viewController.cy_tabBarItem.cy_normalImageN ){
            UIImage *image = [UIImage imageNamed:viewController.cy_tabBarItem.cy_normalImageN];
            width += image.size.width + 5;
        }
    }
    
    UIViewController *viewController = _cy_dataArray[cy_currentIndex];
    if(viewController.cy_tabBarItem.cy_normalImageN){
        UIImage *image = [UIImage imageNamed:viewController.cy_tabBarItem.cy_normalImageN];
        lineWidth += image.size.width + 5;
    }
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(width);
        make.height.mas_equalTo(2);
        make.top.mas_equalTo(48);
        make.width.mas_equalTo(lineWidth + [self stringWidth:viewController.cy_tabBarItem.cy_titleN] + 5);
    }];
    
    if( _didSelected ) {
        [UIView animateWithDuration:0.25 animations:^{
            [_cy_collectionView layoutIfNeeded];
        }];
    }
    
    _preTopTitleCollectionViewCell.titleButton.selected = NO;
    _preTopTitleCollectionViewCell = (CYTopTitleCollectionViewCell *)[_cy_collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:cy_currentIndex inSection:0]];
    _preTopTitleCollectionViewCell.titleButton.selected = YES;
    
    
    if( _itemSpace <= 10 ) {
        [_cy_collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:cy_currentIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}

- (UIView *)lineView {
    if( !_lineView ) {
        _lineView = [[UIView alloc] init];
    }
    return _lineView;
}

@end
